package com.controller;

import com.entity.Products;
import com.entity.Type;
import com.entity.table.ProductsTableDef;
import com.service.ProductsService;
import com.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Objects;

@Controller
public class IndexController {

    @Autowired
    ProductsService productsService;
    @Autowired
    TypeService typeService;

    @GetMapping(value = "")//首页
    public String index(Model model, @RequestParam(value = "typeId", defaultValue = "") String typeId) {
        try {
            List<Type> typeList = typeService.list();//查询分类列表
            List<Products> goods;
            if(!Objects.equals(typeId, "")){
                goods = productsService.queryChain().where(ProductsTableDef.PRODUCTS.TYPE_ID.eq(typeId)).list();
            }
            else {
                 goods = productsService.queryChain().list();
            }
            model.addAttribute("typeList", typeList);
            model.addAttribute("goodsList", goods);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "client/index";
    }
}
